package com.longrise.android.byjk.plugins.tabfirst.revision.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.model.Insurace;
import com.longrise.android.byjk.widget.recyclerviewadapter.SimpleArrayAdapter;

/**
 * Created by sujizhong on 2017/2/21.
 * 健康养生的资讯适配器
 */
public class ConsultationAdapter1 extends SimpleArrayAdapter<Insurace> {

    private static final String TAG = "ConsultationAdapter";

    /**
     * have pic
     */
    private final int mItemViewType_0 = 0;
    private final int mItemViewType_1 = 1;

    private int mCurrent_ItemViewType = mItemViewType_0;

    private LayoutInflater mInflater;

    private boolean mHasFooter;

    public ConsultationAdapter1(BaseActivity activity) {
        mInflater = LayoutInflater.from(activity);
    }

    @Override
    protected BaseRecyclerViewHolder onCreateContentItemViewHolder(ViewGroup parent, int viewType) {
        if (viewType == mItemViewType_0) {
            return new PicViewHolder(mInflater.inflate(R.layout.item_insurance_0, parent, false));
        } else {
            return new ViewHolder(mInflater.inflate(R.layout.item_insurance_1, parent, false));
        }
    }

    @Override
    protected BaseRecyclerViewHolder onCreateFooterItemViewHolder(ViewGroup parent) {
        return new FooterView(mInflater.inflate(R.layout.footerview, parent, false));
    }

    @Override
    protected void onBindContentView(BaseRecyclerViewHolder holder, int position) {
        Insurace insurace = getItem(position);
        if (mCurrent_ItemViewType == mItemViewType_0) {
            parseDataPicViewHolder((PicViewHolder) holder, position, insurace);
        } else {
            parseDataViewHolder((ViewHolder) holder, position, insurace);
        }
    }

    private void parseDataPicViewHolder(PicViewHolder picViewHolder, final int postion, final Insurace item) {
        Glide.with(picViewHolder.itemView.getContext()).load(item.newspic).centerCrop().into(picViewHolder.mImageView);
        picViewHolder.mTextTitle.setText(item.title);
        picViewHolder.mTextFrom.setText(item.newfrom);
        picViewHolder.mTextComment.setText(item.reviewnumstr + "评论");
        picViewHolder.mTextTime.setText(item.createtime);
        picViewHolder.mClickView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener(postion, item);
            }
        });
    }

    private void parseDataViewHolder(ViewHolder viewHolder, final int position, final Insurace item) {
        viewHolder.mTextTitle.setText(item.title);
        viewHolder.mTextFrom.setText(item.newfrom);
        viewHolder.mTextComment.setText(item.reviewnumstr + "评论");
        viewHolder.mTextTime.setText(item.createtime);
        viewHolder.mClickView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener(position, item);
            }
        });
    }

    @Override
    protected int getContentItemViewType(int position) {
        Insurace insurace = getItem(position);
        mCurrent_ItemViewType = TextUtils.isEmpty(insurace.newspic) ? mItemViewType_1 : mItemViewType_0;
        return mCurrent_ItemViewType;
    }

    private class PicViewHolder extends BaseRecyclerViewHolder {

        private ImageView mImageView;
        private TextView mTextTitle;
        private TextView mTextFrom;
        private TextView mTextComment;
        private TextView mTextTime;
        private View mClickView;

        public PicViewHolder(View itemView) {
            super(itemView);
            mClickView = itemView;
            mImageView = (ImageView) itemView.findViewById(R.id.ivpic_insurance_0);
            mTextTitle = (TextView) itemView.findViewById(R.id.tvtitle_insurance_0);
            mTextFrom = (TextView) itemView.findViewById(R.id.tvfrom_insurance_0);
            mTextComment = (TextView) itemView.findViewById(R.id.tvconment_insurance_0);
            mTextTime = (TextView) itemView.findViewById(R.id.tvtime_insurance_0);
        }
    }

    private class ViewHolder extends BaseRecyclerViewHolder {

        private TextView mTextTitle;
        private TextView mTextFrom;
        private TextView mTextComment;
        private TextView mTextTime;
        private View mClickView;

        public ViewHolder(View itemView) {
            super(itemView);
            mClickView = itemView;
            mTextTitle = (TextView) itemView.findViewById(R.id.tvtitle_insurance_1);
            mTextFrom = (TextView) itemView.findViewById(R.id.tvfrom_insurance_1);
            mTextComment = (TextView) itemView.findViewById(R.id.tvconment_insurance_1);
            mTextTime = (TextView) itemView.findViewById(R.id.tvtime_insurance_1);
        }
    }

    private class FooterView extends BaseRecyclerViewHolder {

        public FooterView(View itemView) {
            super(itemView);
        }
    }

    @Override
    protected boolean hasFooter() {
        return mHasFooter;
    }

    public void onLoadMoreInsert() {
        mHasFooter = true;
    }

    public void onLoadMoreComplete(){
        notifyItemRemoved(getItemCount());
    }

    public void onItemClickListener(int position, Insurace insurace) {
    }
}
